//
//  DKRegisterViewController.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/10/9.
//

#import "DKRegisterViewController.h"
#import "DKLoginRegisterNameView.h"
#import "DKLoginRegisterAccountView.h"
#import "DKLoginPhoneView.h"
#import "DKLoginAgreementAlertView.h"
#import "DKLoginRegisterViewController.h"
#import "DKRegisterHttp.h"
#import "DKLoginViewController.h"
@interface DKRegisterViewController ()<TTTAttributedLabelDelegate>
@property (nonatomic,strong)DKLoginPhoneView * dk_LoginPhoneView;
@property (nonatomic, strong)UILabel * dk_LoginAlertLab;
@property (nonatomic, strong)UIButton * dk_RegisterNextBtn;
@property (nonatomic, strong)UIButton * dk_RegisterAgreementBtn;
@end
@implementation DKRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    // Do any additional setup after loading the view.
    [self dk_creatView];
    if(self.isNeedBack){
        [self makeNavBack];
    }
    [DKGlobalHelper defaultManager].registeredStartTime = [NSString getCurrentTimeTimestamp];
    [self requestUserBrhavRecord:DK_ActionEnter withParmDic:@{}];
}
- (void)dk_creatView{
    WeakSelf(self)
    //log名称
    DKLoginRegisterNameView *loginRegisterNameView = [[DKLoginRegisterNameView alloc] init];
    [self.view addSubview:loginRegisterNameView];
    [loginRegisterNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.height.equalTo(@(DK_IsWidthScale(40)));
    }];
    //注册提醒
    UILabel *dk_RegisterAlertLab = [[UILabel alloc] init];
    dk_RegisterAlertLab.textColor = DK_Hex(@"#333333");
    dk_RegisterAlertLab.font = DK_FONT_Medium(17);
    dk_RegisterAlertLab.text = @"Register with mobile number";
    [self.view addSubview:dk_RegisterAlertLab];
    [dk_RegisterAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
        make.top.equalTo(loginRegisterNameView.mas_bottom).offset(DK_IsWidthScale(30));
    }];
    //去账号登录
    DKLoginRegisterAccountView *dk_LoginRegisterAccountView = [[DKLoginRegisterAccountView alloc] init];
    [self.view addSubview:dk_LoginRegisterAccountView];
    [dk_LoginRegisterAccountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(dk_RegisterAlertLab.mas_bottom).offset(DK_IsWidthScale(15));
        make.height.equalTo(@(DK_IsWidthScale(21)));
    }];
    dk_LoginRegisterAccountView.goAccountLoginBlock = ^{//去账号登录
        DKLoginViewController * dk_LoginViewController = [[DKLoginViewController alloc] init];
        [weakSelf.navigationController pushViewController:dk_LoginViewController animated:NO];
    };
    //账号
    self.dk_LoginPhoneView = [[DKLoginPhoneView alloc] init];
    self.dk_LoginPhoneView.loginPlace = @"Please enter your phone number";
    [self.view addSubview:self.dk_LoginPhoneView];
    [self.dk_LoginPhoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_LoginRegisterAccountView.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_LoginPhoneView.goCheckPhoneBlock = ^(BOOL isPhoneNull) {
        //检查输入手机号是否是空
        [weakSelf updataNextStatus];
        [weakSelf requestUserBrhavRecord:DK_ActionPhone_Input withParmDic:@{}];
    };
    self.dk_LoginPhoneView.StartEditPhoneBlock = ^{//开始编辑
        [weakSelf requestUserBrhavRecord:DK_ActionPhone_Focus withParmDic:@{}];
    };
    //协议
    self.dk_RegisterAgreementBtn = [[UIButton alloc] init];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_noSelect"] forState:UIControlStateNormal];
    [self.dk_RegisterAgreementBtn setBackgroundImage:[UIImage imageNamed:@"dk_register_Select"] forState:UIControlStateSelected];
    [self.dk_RegisterAgreementBtn addTarget:self action:@selector(goAgreeAgreenment:) forControlEvents:UIControlEventTouchUpInside];
    self.dk_RegisterAgreementBtn.selected = YES;
    [self.view addSubview:self.dk_RegisterAgreementBtn];
    [self.dk_RegisterAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(36)));
        make.width.equalTo(@(DK_IsWidthScale(16)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
        make.bottom.equalTo(@(-DK_IsWidthScale(77)-DK_SafeAreaBottom));
    }];
    TTTAttributedLabel * dk_RegisterAgreementLab = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
    dk_RegisterAgreementLab.numberOfLines = 0;
    [self.view addSubview:dk_RegisterAgreementLab];
    [dk_RegisterAgreementLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_RegisterAgreementBtn.mas_right).offset(DK_IsWidthScale(6));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_RegisterAgreementBtn.mas_top).offset(-DK_IsWidthScale(1));
    }];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"Account login is deemed to agree with the《Privacy Agreement》and the 《Registration Agreement》"];
    [attStr addAttribute:NSFontAttributeName value:DK_FONT_Regular(12) range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#333333") range:NSMakeRange(0,attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(41,19)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DK_Hex(@"#4566FF") range:NSMakeRange(68,attStr.length-68)];
    dk_RegisterAgreementLab.delegate = self;
    dk_RegisterAgreementLab.attributedText = attStr;
    dk_RegisterAgreementLab.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    dk_RegisterAgreementLab.linkAttributes = @{NSForegroundColorAttributeName:DK_Hex(@"#4566FF")};
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_PrivacyAgreement] withRange:NSMakeRange(41,19)];
    [dk_RegisterAgreementLab addLinkToURL:[NSURL URLWithString:DK_RegisterAgreement] withRange:NSMakeRange(68,attStr.length-68)];
    //next
    self.dk_RegisterNextBtn = [[UIButton alloc] init];
    [self.dk_RegisterNextBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateSelected];
    self.dk_RegisterNextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateNormal];
    [self.dk_RegisterNextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateSelected];
    [self.dk_RegisterNextBtn addTarget:self action:@selector(goLogin) forControlEvents:UIControlEventTouchUpInside];
    self.dk_RegisterNextBtn.userInteractionEnabled = false;
    [self.view addSubview:self.dk_RegisterNextBtn];
    [self.dk_RegisterNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(dk_RegisterAgreementLab.mas_top).offset(-DK_IsWidthScale(100));
    }];
}
- (void)updataNextStatus{
    NSString * account = [self.dk_LoginPhoneView getLoginPhone];
    if(account.length&&self.dk_RegisterAgreementBtn.isSelected){
        self.dk_RegisterNextBtn.selected = YES;
        self.dk_RegisterNextBtn.userInteractionEnabled = YES;
    }else{
        self.dk_RegisterNextBtn.selected = NO;
        self.dk_RegisterNextBtn.userInteractionEnabled = NO;
    }
}
//选择协议
- (void)goAgreeAgreenment:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    [self updataNextStatus];
}
- (void)showSureAgreemnetView{
    WeakSelf(self)
    [DKLoginAgreementAlertView dk_ShowLoginAgreementAlertViewNext:self.view Complete:^{
        weakSelf.dk_RegisterAgreementBtn.selected = YES;        
    } WithGoUrl:^(NSString * _Nonnull url) {
//        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//        dk_WebViewCtrl.webUrl = url;
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:url withIsNavBar:YES];

        [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    }];
}
- (void)goLogin{
    WeakSelf(self)
    if([self.dk_LoginPhoneView getLoginPhone].length <= 0){
        [DKAlertHelper showMessage:@"Please enter your phone number"];
        return;
    }
    if(!self.dk_RegisterAgreementBtn.isSelected){
        [self showSureAgreemnetView];
        return;
    }
    //判断手机号是否存在
    NSMutableDictionary *registerPhoneDic = [[NSMutableDictionary alloc] init];
    [registerPhoneDic setValue:[self.dk_LoginPhoneView getLoginPhone] forKey:@"phone"];
    [DKRegisterHttp apiPhoneExistForAPI:registerPhoneDic complete:^(BOOL isPhoneExist) {
        if(isPhoneExist){
            [DKAlertHelper showMessage:@"Mobile number already registered"];
        }else{
            DKLoginRegisterViewController * dk_LoginRegisterViewCtrl = [[DKLoginRegisterViewController alloc] init];
            dk_LoginRegisterViewCtrl.dk_registerPhone = [self.dk_LoginPhoneView getLoginPhone];
            [self.navigationController pushViewController:dk_LoginRegisterViewCtrl animated:YES];
        }
        NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
        [parmDic setObject:[weakSelf.dk_LoginPhoneView getLoginPhone] forKey:@"phone"];
        [parmDic setObject:@(isPhoneExist) forKey:@"exist"];
        [weakSelf requestUserBrhavRecord:DK_ActionCheckPhone_Exist withParmDic:parmDic];
    }];
}
- (void)requestUserBrhavRecord:(NSString *)action withParmDic:(NSDictionary *)parmDic{
    [DKPublicHttp apiUserBrhavRecordForAPI:parmDic withPageName:DK_Register withAction:action complete:^{
            
    } fail:^{
            
    }];
}
#pragma mark -- TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
//    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//    dk_WebViewCtrl.webUrl = url.absoluteString;
//    
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:url.absoluteString withIsNavBar:YES];

    [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
@end
